<?php
/**
 * Created by openXtiger.org.
 * User: xtiger
 * Date: 2009-6-20
 * Time: 11:43:44
 */
function cms_categorymodel_select($category, $parentid = 0, $name = 'catid',
        $id ='', $alt = '', $catid = 0, $property = '', $type = 0, $optgroup = 0) {
    if(!$id) $id = $name;
    $tree = xt_load('library','tree',TRUE);
    $optgroup_str = '';
    if($optgroup) $optgroup_str = "<optgroup label='\$name'></optgroup>";
    $data = "<select name='$name' id='$id' $property>\n<option value='0'>$alt</option>\n";
    if(is_array($category)) {
        $categorys = array();
        foreach($category as $id=>$cat) {
            $categorys[$id] = array('id'=>$id, 'parentid'=>$cat['parentid'], 'name'=>$cat['catname']);
        }
        $tree->tree($categorys);
        $data .= $tree->get_tree($parentid, "<option value='\$id' \$selected>\$spacer\$name</option>\n", $catid, '' , $optgroup_str);
    }
    $data .= '</select>';
    return $data;
}

function cms_categorymodel_listinfo($parentid = -1) {
    $db = xt_load('db','icweb');
    $categorys = array();
    $where = $parentid > -1 ? " WHERE parentid='$parentid'" : '';
    $result = $db->query("SELECT * FROM `".xt_tname('cmscategory','icweb')."`   $where ORDER BY `listorder`,`catid`");
    while($r = $db->fetch_array($result)) {
        $categorys[$r['catid']] = $r;
    }
    $db->free_result($result);
    return $categorys;
}

function cms_categorymodel_get($catid) {
    $db = xt_load('db','icweb');
    $data = $db->fetch_first("SELECT * FROM `".xt_tname('cmscategory','icweb')."` WHERE `catid`=$catid");
    if(!$data) return false;
    return $data;
}
function cms_categorymodel_add($category) {
     $db = xt_load('db','icweb');
     $catid = $db->insert('cmscategory',$category,1,'icweb');
     $cachemodel = xt_load('model','cms.cachemodel');
     $cachemodel('reflashCommon');
     return $catid;    
}
function cms_categorymodel_edit($catid, $category, $pid) {
     $db = xt_load('db','icweb');
     $r = $db->update('cmscategory',$category,"`catid`=$catid", 'icweb');
     $cachemodel = xt_load('model','cms.cachemodel');
     $cachemodel('reflashCommon');
     return $r;
}
?>